﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DAL;


namespace BLL
{
    public class MenusManager
    {

        private static EFHelper<OASystemEntities> _efHelper = new EFHelper<OASystemEntities>();

        #region 根据当前登陆者ID获取该用户所拥有哪些菜单权限 注意： 超级管理员拥有所有
        /// <summary>
        /// 根据当前登陆者ID获取该用户所拥有哪些菜单权限
        ///注意： 超级管理员拥有所有
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static List<OA_Menus> QueryListByUserId(int UserId)
        {
            //获取当前登录用户信息
            var userInfo = _efHelper.GetInfo<OA_UserAccount>(u => u.Id == UserId);

            //超管可以查看所有菜单
            if (userInfo.IsSuper)
                return _efHelper.QueryList<OA_Menus>(null).OrderBy(m => m.UpdateTime).ToList();

            List<OA_Role> roleList = RoleManager.GetRoleByUserId(UserId);

            List<OA_Menus> menusList = new List<OA_Menus>();
            try
            {
                if (roleList.Count != 0)
                {
                    foreach (var item in roleList)
                    {
                        var AuthorityList = _efHelper.QueryList<OA_Authority>(a => a.RoleId == item.Id);
                        if (AuthorityList.Count != 0)

                            foreach (var a in AuthorityList)
                            {
                                var menu = _efHelper.GetInfo<OA_Menus>(m => m.Id == a.MenuId);
                                menusList.Add(menu);
                            } 
                    }

                }
                return menusList.OrderBy(m => m.UpdateTime).ToList();

            }
            catch (Exception)
            {
                return new List<OA_Menus>();
            }

        }
        #endregion


        #region 根据父级ID查询返回子级集合
        /// <summary>
        /// 根据父级ID查询返回子级集合
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public static List<OA_Menus> QueryListByParentId(string parentId)
        {
            try
            {
                //排除不是当前登陆者可以操作的菜单
                //获取当前登录用户信息
                var userInfo = System.Web.HttpContext.Current.Session["info"] as Model.OA_UserAccount;
                if (userInfo.IsSuper)
                    return _efHelper.QueryList<OA_Menus>(m => m.ParentId == parentId).ToList();
                List<OA_Role> roleList = RoleManager.GetRoleByUserId(userInfo.Id);

                //查询本属于当前登录用户所拥有的的操作菜单集合
                List<OA_Menus> menusList = new List<OA_Menus>();
                if (roleList.Count != 0)
                {
                    foreach (var item in roleList)
                    {
                        var AuthorityList = _efHelper.QueryList<OA_Authority>(a => a.RoleId == item.Id);
                        if (AuthorityList.Count != 0)

                            foreach (var a in AuthorityList)
                            {
                                var menu = _efHelper.GetInfo<OA_Menus>(m => m.Id == a.MenuId);
                                menusList.Add(menu);
                            }
                    }

                }
                 //根据ParentId查询该父级下面所有的子菜单集合
                var childList=_efHelper.QueryList<OA_Menus>(m => m.ParentId == parentId).ToList();

                //获取两个集和的交集 
               return menusList.Intersect<OA_Menus>(childList).ToList();

            }
            catch (Exception ex)
            {
                return new List<OA_Menus>();
            }
        }
        #endregion


        #region 查询所有的菜单集合
        /// <summary>
        /// 查询所有的菜单集合
        /// </summary>
        /// <returns></returns>
        public static List<OA_Menus> QueryList()
        {
            try
            {
                return _efHelper.QueryList<OA_Menus>(null);
            }
            catch (Exception)
            {
                return new List<OA_Menus>();
            }
        }
        #endregion


        #region 根据菜单ID返回制定对象
        public static OA_Menus GetEntity(string menuId)
        {
            try
            {
                return _efHelper.GetInfo<OA_Menus>(m => m.Id == menuId);
            }
            catch (Exception)
            {
                return new OA_Menus();
            }

        }
        #endregion


        #region 新增菜单
        public int Add(string Name, bool IsParent, string Url, string ParentId)
        {
            try
            {
                OA_Menus menu = new OA_Menus()
                {
                    Id = Guid.NewGuid().ToString().Replace("-","").ToUpper(),
                    Name = Name,
                    IsParent = IsParent,
                    InsertTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                };
                if (IsParent)
                {

                    menu.Url = string.Empty;
                    menu.ParentId = string.Empty;
                    var orderindex = _efHelper.QueryList<OA_Menus>(null).OrderByDescending(m => m.OrderIndex).FirstOrDefault().OrderIndex;
                    orderindex++;
                    menu.OrderIndex = orderindex;
                }
                else
                {

                    menu.Url = Url;
                    menu.ParentId = ParentId;
                    var orderindex = _efHelper.QueryList<OA_Menus>(m => m.ParentId == ParentId).OrderByDescending(m => m.OrderIndex).FirstOrDefault().OrderIndex;
                    orderindex++;
                    menu.OrderIndex = orderindex;
                }


                return _efHelper.Insert<OA_Menus>(menu);

            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        #endregion

        #region 根据角色查询该角色可以操作的菜单集合
        public static List<OA_Menus> QueryListByRoleId(int roleId)
        {
            try
            {
                var authorList = _efHelper.QueryList<OA_Authority>(a => a.RoleId == roleId).ToList();
                List<OA_Menus> menuList = new List<OA_Menus>();
                if (authorList.Count != 0)
                {
                    foreach (var item in authorList)
                    {
                        var menu = _efHelper.GetInfo<OA_Menus>(m => m.Id == item.MenuId);
                        if (menu != null)
                        {
                            menuList.Add(menu);
                        }

                    }
                }
                return menuList;
            }
            catch (Exception ex)
            {
                
                throw;
            }

        }
        #endregion

    }
}
